﻿using System.Data;
using MySql.Data.MySqlClient;
using MyTech.DataAccess.Common;
using System.Configuration;

namespace MyTech.DataAccess.MySQL
{
    public class DataAccessMySQL : DataAccessSQLBase, IDatabase
    {        
        public  DataAccessMySQL() 
        {
            connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;

            if (null != connectionString)
            {
                _connection = new MySqlConnection(connectionString);
            }        
        }

        public int? ExecuteNonQuery(string sqlStmt)
        {
            int? response = null;
            //Create the command object
            var command = new MySqlCommand(sqlStmt);
            response = ExecuteNonQuery(command);
            return response;
        }

        public DataSet ExecuteReader(string sqlStmt)
        {            
            //Create the command object
            var command = new MySqlCommand(sqlStmt);
            var response = ExecuteReader(command);
            return response;
        }

        public DataSet ExecuteReader(string sqlStmt, SpParams spParams)
        {
            //Create the command object
            var command = new MySqlCommand(sqlStmt);
            command.CommandType = CommandType.StoredProcedure;

            if (null != spParams && null != spParams.Parameters && spParams.Parameters.Count > 0)
            {
                foreach (var item in spParams.Parameters)
                {
                    var paramName = string.Format("?{0}", item.ParamName);
                    command.Parameters.AddWithValue(paramName, item.ParamValue);
                    command.Parameters[paramName].Direction = item.ParamDirection;
                }
            }

            var response = ExecuteReader(command);
            return response;
        }

        public object ExecuteScalar(string sqlStmt)
        {
            object response = null;
            //Create the command object
            var command = new MySqlCommand(sqlStmt);
            response = ExecuteScalar(command);
            return response;
        }
        public DbObjectType DbType
        {
            get
            {
                return DbObjectType.MsSQL;
            }
        }
    }
}
